﻿
package test {
	import extremefx.unitTest.Trace;
	import extremefx.TimeSpan;
	import extremefx.events.EventArgs;
	import extremefx.events.ObjectEventArgs;
	import extremefx.events.ProgressEventArgs;
	import extremefx.modules.VideoPlayer;
	
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;		

	/**
	 * @author Marcelo Volmaro
	 */
	[Frame(factoryClass="extremefx.Preloader")];
	public class VideoPlayerTest extends Sprite {

		private var _tf:TextField;
		private var _sp:VideoPlayer;

		public function main():void {
			// cover the stage with a green rectangle
			
			var greenRect:Sprite = new Sprite();
			greenRect.graphics.beginFill(0x00FF00);
			greenRect.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			addChild(greenRect);

			// create red square on stage, turn it into a button for going to full screen
			
			var redSquare:Sprite = new Sprite();
			redSquare.graphics.beginFill(0xFF0000);
			redSquare.graphics.drawRect(0, 0, 300, 300);
			redSquare.x = 50;
			redSquare.y = 50;
			redSquare.addEventListener(MouseEvent.CLICK, enterFullScreen);
			redSquare.buttonMode = true;
			addChild(redSquare);


			var tf:TextField = _tf = new TextField();
			var tfmt:TextFormat = new TextFormat("arial unicode ms", 14, 0);
			stage.addEventListener(Event.RESIZE, resizeStage);
			//			addChild(tf);
			tf.x = 0;
			tf.y = 0;
			tf.width = stage.stageWidth;
			tf.height = stage.stageHeight;
			tf.defaultTextFormat = tfmt;
			
			_sp = new VideoPlayer();
			_sp.attachToDisplay(this);
			//			_sp.setBounds(stage.width-360, 0);
			//			_sp.setSize(640, 480);
			_sp.setUrl("PaulRothemund_2008-[None].mp4");
			
			_sp.onComplete.add(onComplete);
			_sp.onMetaData.add(onMetadata);
			_sp.onOpen.add(onOpen);
			_sp.onPlayComplete.add(onPlayComplete);
			_sp.onProgress.add(onProgress);
			//			_sp.useHighQuality = true;
			_sp.play();
			_sp.position = new TimeSpan(0, 8, 0);
		}

		private function enterFullScreen(e:MouseEvent):void {
			// we will go to full screen zoomed in on the red square
			var redSquare:Sprite = e.target as Sprite;
			var fullScreenRect:Rectangle = new Rectangle(redSquare.x, redSquare.y, redSquare.width, redSquare.height);

			// calculate aspect ratio of the red square
			var rectAspectRatio:Number = fullScreenRect.width / fullScreenRect.height;

			// calculate aspect ratio of the screen
			var screenAspectRatio:Number = stage.fullScreenWidth / stage.fullScreenHeight;

			// change the fullScreenRect so that it covers the entire screen, keeping it centered on the redSquare
			// try commenting out this section to see what happens if you do not fix the aspect ratio.
			if (rectAspectRatio > screenAspectRatio) {
				var newHeight:Number = fullScreenRect.width / screenAspectRatio;
				fullScreenRect.y -= ((newHeight - fullScreenRect.height) / 2);
				fullScreenRect.height = newHeight;
			} else if (rectAspectRatio < screenAspectRatio) {
				var newWidth:Number = fullScreenRect.height * screenAspectRatio;
				fullScreenRect.x -= ((newWidth - fullScreenRect.width) / 2);
				fullScreenRect.width = newWidth;
			}

			// go to full screen
			stage.fullScreenSourceRect = fullScreenRect;
			stage.displayState = StageDisplayState.FULL_SCREEN;
		}

		private function onProgress(pSender:VideoPlayer, pArgs:ProgressEventArgs):void {
			_tf.appendText("on Progress:" + pArgs.percent);
			_tf.appendText("\n");
		}

		private function onPlayComplete(pSender:VideoPlayer, pArgs:EventArgs):void {
			_tf.appendText("onPlayComplete\n");
		}

		private function onOpen(pSender:VideoPlayer, pArgs:EventArgs):void {
			_tf.appendText("onOpen\n");
		}

		private function onMetadata(pSender:VideoPlayer, pArgs:ObjectEventArgs):void {
			_tf.appendText("onMetadata\n");
			_tf.appendText(_sp.duration + "\n");
			_tf.appendText(Trace.obj(pArgs.value));
			_tf.appendText("\n");
		}

		private function onComplete(pSender:VideoPlayer, pArgs:EventArgs):void {
			_tf.appendText("onComplete\n");
		}

		private function resizeStage(event:Event):void {
			_tf.width = stage.stageWidth;
			_tf.height = stage.stageHeight;
		}
	}
}
